package cn.new7as.cas5s.ui.apply;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.R;

import cn.new7as.cas5s.databinding.ActivityEditforthBinding;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.EditBankSendApi;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.GetBaseDataApi;
import cn.new7as.cas5s.ui.easy.netapi.SaveBaseDataApi;
import cn.new7as.cas5s.ui.easy.netapi.UpdateBankApi;


public class EditForthActivity extends BaseActivity {

    private ActivityEditforthBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityEditforthBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("Edit Bank Card Details");
        show();
        getBase();
        initV();

    }


    public static final int CHANGE_TIPS_TIMER_INTERVAL = 1000;
    private Handler mChangeTipsHandler = new Handler(Looper.getMainLooper());
    int i = 60;

    private void startMethod4() {
        binding.send.setEnabled(false);
        Runnable mChangeTipsRunnable = new Runnable() {
            @Override
            public void run() {
                i--;
                binding.send.setText(i + "S");

                if (i == 0) {
                    binding.send.setText("Resend");
                    binding.send.setEnabled(true);
                    binding.send.setTextColor(getResources().getColor(R.color.theme));
                } else
                    mChangeTipsHandler.postDelayed(this, CHANGE_TIPS_TIMER_INTERVAL);
            }
        };

        mChangeTipsHandler.post(mChangeTipsRunnable);
    }

    void send(String pho) {
        EasyHttp.post(this)
                .api(new EditBankSendApi(getunique())).request(new OnHttpListener<Object>() {
                    @Override
                    public void onHttpSuccess(@NonNull Object result) {
                        binding.send.setTextColor(getResources().getColor(R.color.text));
                        i = 60;
                        startMethod4();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }

    @Override
    public void initV() {
        super.initV();

        setEtListener(binding.ifscBa, binding.accountnumberBa, binding.accountnumberconfirmationBa);
        binding.send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ph = Utils.getphone(EditForthActivity.this);
                if (ph.length() == 10)
                    send(ph);
                else
                    toa(getString(R.string.phone_wrong));
            }
        });
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ifscBa = binding.ifscBa.getText().toString();
                String accountnumberBa = binding.accountnumberBa.getText().toString();
                String accountnumberconfirmationBa = binding.accountnumberconfirmationBa.getText().toString();
                String upiBa = binding.upiBa.getText().toString();
                String send = binding.phoneotp.getText().toString();

                SaveBaseDataApi.Data data = new SaveBaseDataApi().new Data();
                data.type = 2;
                SaveBaseDataApi.Data4 data1 = new SaveBaseDataApi().new Data4();
                data1.code = ifscBa;
                data1.number = accountnumberBa;
                data1.confirmNumber = accountnumberconfirmationBa;
                data1.upi = upiBa;
                data.bankDetailInfo = data1;
                show();
                EasyHttp.post(EditForthActivity.this)
                        .api(new UpdateBankApi(getunique(), ifscBa, send, upiBa, accountnumberBa)).request(new OnHttpListener<HttpData<Object>>() {
                            @Override
                            public void onHttpSuccess(@NonNull HttpData<Object> result) {
                                hide();

                                finish();

                            }

                            @Override
                            public void onHttpFail(@NonNull Throwable throwable) {
                                deal(throwable);

                            }
                        });
            }


        });
    }

    void getBase() {
        EasyHttp.post(EditForthActivity.this)
                .api(new GetBaseDataApi(getunique(), 2)).request(new OnHttpListener<HttpData<GetBaseDataApi.Data>>() {


                    @Override
                    public void onHttpSuccess(@NonNull HttpData<GetBaseDataApi.Data> result) {
                        hide();
                        if (result.getResult() != null) {
                            binding.ifscBa.setText(result.getResult().code);
                            binding.accountnumberBa.setText(result.getResult().number);
                            binding.accountnumberconfirmationBa.setText(result.getResult().number);
                            binding.upiBa.setText(result.getResult().upi);

                            setTrue();
                        }
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);

                    }
                });
    }

    @Override
    public void setFalse() {
        super.setFalse();
        binding.verifyme.setEnabled(false);
    }

    @Override
    public void setTrue() {
        super.setTrue();
        if (Utils.isStr(binding.ifscBa, binding.accountnumberBa, binding.accountnumberconfirmationBa))
            binding.verifyme.setEnabled(true);
    }


}